<?php
/**
 * Created by PhpStorm.
 * User: zouyanan
 * Date: 2019/5/11
 * Time: 下午10:35
 */

namespace app\userapi\controller;
use app\common\controller\Api;

class House extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = '*';



    /*
     * 房源详情
     */
    public function detail(){
        $userInfo = $this->auth->getUserinfo();
        $house_id = $this->request->request("house_id");

        db('yjy_view')->insert([
            'user_id'=>$userInfo['id'],
            'house_id'=>$house_id,
            'createtime' =>time()
        ]);
        db('user_renter')->where('user_id',$userInfo['id'])->setInc('num_view',1);


        $info = db("yjy_house a")
            ->join('user b','a.user_id = b.id','LEFT')
            ->join('user_agent c','b.id = c.user_id','LEFT')
            ->field("a.*,b.id as agent_id,b.nickname,b.avatar,b.mobile,c.fans")
            ->where("a.id",$house_id)
            ->find();

        //判断关注:0=否,1=是
        $attention = db('yjy_attention')
            ->where('user_id',$userInfo['id'])
            ->where('agent_id',$info['agent_id'])
            ->value('id');
        if($attention){
            $info['is_attention'] = 1;
        }else{
            $info['is_attention'] = 0;
        }

        $houseList_agent = db('yjy_house')->field('id,image,title,location_near,price')->where('id',$info['agent_id'])->select();
        $data = [
            'info' => $info,
            'houseList_agent' => $houseList_agent
        ];
        $this->success("返回成功",$data);

    }


    /**
     * 收藏/取消收藏
     */
    public function collect(){
        $userInfo = $this->auth->getUserinfo();
        $house_id = $this->request->request("house_id");
        $action =  $this->request->request("action");  //添加收藏=add,取消收藏=cancel
        if(!$action || !$house_id){
            $this->error("缺少参数");
        }
        if($action == 'add'){
            $res_a = db('yjy_collect')->insert([
                'user_id'  => $userInfo['id'],
                'house_id' => $house_id,
                'createtime'=> time()
            ]);
            $res_b = db('user_renter')->where('user_id',$userInfo['id'])->setInc('num_collect',1);
            $msg = '收藏';
        }elseif($action == 'cancel'){
            $res_a = db('yjy_collect')->where('user_id',$userInfo['id'])->where('house_id',$house_id)->delete();
            $res_b = db('user_renter')->where('user_id',$userInfo['id'])->setDec('num_collect',1);
            $msg = '收藏取消';
        }
        if($res_a && $res_b){
            $this->success($msg."成功");
        }else{
            $this->error($msg."失败");
        }
    }



    /**
     *
     */

































}